package com.wyait.manage.web.carousel;


import com.wyait.manage.pojo.Carousel;
import com.wyait.manage.pojo.User;
import com.wyait.manage.service.CarouselService;
import com.wyait.manage.utils.PageDataResult;
import com.wyait.manage.web.menu.MenuController;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/carousel")
public class CarouselController {
    @Autowired
    private CarouselService carouselService;

    private static final Logger logger = LoggerFactory
            .getLogger(MenuController.class);

    /**
     * 进入轮播图管理页面
     */
    @RequestMapping("/carouselList")
    public String menuList(){
        return "carousel/carouselList";
    }

    /**
     * 分页查询轮播图支持筛选加分页
     */
    @RequestMapping("/getCarouselList")
    @ResponseBody
    public PageDataResult getMenuList(@RequestParam("page") Integer page,
                                      @RequestParam("limit") Integer limit, Carousel carousel){
        logger.debug("分页查询轮播图列表！搜索条件：carousel：" + carousel + ",page:" + page
                + ",每页记录数量limit:" + limit);
        PageDataResult pdr = new PageDataResult();
        try {
            if (null == page) {
                page = 1;
            }
            if (null == limit) {
                limit = 10;
            }
            // 获取轮播图列表
            pdr = carouselService.getCaroutleList(carousel, page, limit);
            logger.debug("轮播图列表查询=pdr:" + pdr);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("轮播图列表查询异常！", e);
        }

        return pdr;
    }

    /**
     * 轮播图[新增或更新]
     */
    @PostMapping("/setCarousel")
    @ResponseBody
    public String setMenu(Carousel carousel){
        logger.debug("设置轮播图[新增或更新]！carousel:" + carousel );
        try {

            if (null == carousel) {
                logger.debug("轮播图[新增或更新]，结果=请您填写事项信息");
                return "您填写轮播图信息";
            }
            User existUser = (User) SecurityUtils.getSubject().getPrincipal();
            if (null == existUser) {
                logger.debug("设置轮播图[新增或更新]，结果=您未登录或登录超时，请您登录后再试");
                return "您未登录或登录超时，请您登录后再试";
            }

            // 设置用户[新增或更新]
            logger.info("设置轮播图[新增或更新]成功！carousel=" + carousel + "，操作的用户ID=" + existUser.getId());
            return carouselService.setCarousel(carousel);
        }catch (Exception e) {
            e.printStackTrace();
            logger.error("设置轮播图[新增或更新]异常！", e);
            return "操作异常，请您稍后再试";
        }
    }

    /**
     *根据id删除轮播图
     */
    @PostMapping("/delCarousel")
    @ResponseBody
    public String delCarousel(Integer id){
        logger.debug("删除轮播图！id:" + id );
        try{
            return carouselService.delCarousel(id);
        }catch (Exception e) {
            e.printStackTrace();
            logger.error("设置轮播图[删除]异常！", e);
            return "操作异常，请您稍后再试";
        }

    }

    /**
     * 获取轮播图列表
     * @return
     */
    @ResponseBody
    @GetMapping("/getCarousels")
    public List<Carousel> getCarousels(){
        return carouselService.getCarousels();
    }


    /**
     * 图片上传
     */
    @PostMapping("/uploadImg")
    @ResponseBody
    public Map<String,Object> uploadImage(@RequestParam("file") MultipartFile file){
        logger.debug("上传图片！上传的图片名称:" + file.getOriginalFilename());

        //service返回的图片路径和名称
        Map<String, String> dataMap = null;
        //图片返回的json格式
        HashMap<String, Object> map = new HashMap<>();
        try {
            dataMap = carouselService.save(file);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("图片上传失败！", e);
            map.put("code",1);
            map.put("msg","图片上传失败");
            map.put("data",dataMap);
            return map;
        }


        map.put("code","0");
        map.put("msg","图片上传成功");
        map.put("data",dataMap);
        return map;
    }




}
